﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BSCMFF.Core.DataAccessLayer;
using BSCMFF.Core.EmailProvider;
using BSCMFF.Core;
using System.Web.Security;
using Telerik.Web.UI;
using BSCMFF.Core.Exceptions.Database;
using BSCMFF.Core.BusinessLayer;

namespace BSCMFF.Web.Pages.Administration
{
    public partial class EmployeeInfo : MultiLanguagePage
    {
        protected static User selectedUser;
        //private BSCEntities context

        public int SelectedUserID 
        {
            get
            {
                return (selectedUser != null) ? selectedUser.ID : 0;

            }
            //set
            //{

            //}
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                if (Request.QueryString["EmployeeID"] == null)
                {
                    UserManager um = new UserManager();
                    User u = um.GetUserByLogin(User.Identity.Name);
                    Response.Redirect("EmployeeInfo.aspx?EmployeeID=" + u.ID.ToString());
                }
                int employeeID = Convert.ToInt32(Request.QueryString["EmployeeID"]);
                // LabelName.Text = s;

                //if (!IsPostBack)
                //{
                BSCEntities context = new BSCEntities();
                selectedUser = (from user in context.Users.Include("SBU").Include("User2")
                                //where user.Login == User.Identity.Name
                                where user.ID == employeeID
                                select user).SingleOrDefault();



                if (selectedUser == null)
                    return;

                //if (selectedUser.Photo != null)
                //{
                //    RadBinaryImage1.DataValue = selectedUser.Photo;
                //}
                //else
                //{
                //    RadBinaryImage1.ImageUrl = "~/Images/silhouette/silhouette-stewie.jpg";
                //}

                if (selectedUser.Birthdate != null)
                {
                    LabelCurrentBirthday.Text = ((DateTime)selectedUser.Birthdate).ToShortDateString();
                    RadDatePickerCurrentBirthday.SelectedDate = selectedUser.Birthdate;
                }

                if (selectedUser.User2 != null)
                {
                    LinkButtonManager.Text = selectedUser.User2.Fullname;
                    //LinkButtonManager.PostBackUrl = "EmployeeInfoOld.aspx?EmployeeID=" + selectedUser.User2.ID;
                    LinkButtonManager.Click += new EventHandler(LinkButtonManager_Click);
                }

                if (selectedUser.Login == User.Identity.Name)
                {
                    RadButtonUpdateInfo.Visible = true;
                }

                PlaceHolderEmployeeInfo.DataBind();
                //EmployeeName.DataBind();

                // context.Detach(selectedUser);
            }
            else
            {
                //if (selectedUser != null)
                //{
                //    this.DataBind();
                //}
            }
        }

        public void LinkButtonManager_Click(object sender, EventArgs e)
        {
            Response.Redirect("EmployeeInfo.aspx?EmployeeID=" + selectedUser.User2.ID.ToString());
        }

        protected void SaveImage()
        {
            if (RadUpload1.UploadedFiles.Count == 1)
            {                
                long length = RadUpload1.UploadedFiles[0].InputStream.Length;
                //u.Photo = new byte[length];
                byte[] buff = new byte[length];
                RadUpload1.UploadedFiles[0].InputStream.Read(buff, 0, (int)length);
                //RadUploadImage.UploadedFiles[0].

                selectedUser.Photo = buff;
                //BSCEntities context = new BSCEntities();
                //try
                //{
                //    context.SaveChanges();
                //}
                //catch
                //{
                //    throw new InconsistentDatabaseException("Couldn't save image");
                //}
                RadBinaryImage1.DataValue = selectedUser.Photo;
            }
        }

        protected void RadButtonUpdateInfo_Click(object sender, EventArgs e)
        {
            SwitchVisibility();

        }

        private void SwitchVisibility()
        {
            RadButtonSave.Visible = !RadButtonSave.Visible;
            RadButtonCancel.Visible = !RadButtonCancel.Visible;
            RadButtonUpdateInfo.Visible = !RadButtonUpdateInfo.Visible;

            RadUpload1.Visible = !RadUpload1.Visible;

            LabelCurrentPhone.Visible = !LabelCurrentPhone.Visible;
            LabelCurrentEmail.Visible = !LabelCurrentEmail.Visible;
            LabelCurrentBirthday.Visible = !LabelCurrentBirthday.Visible;

            RadTextBoxCurrentPhone.Visible = !RadTextBoxCurrentPhone.Visible;
            RadTextBoxCurrentEmail.Visible = !RadTextBoxCurrentEmail.Visible;
            RadDatePickerCurrentBirthday.Visible = !RadDatePickerCurrentBirthday.Visible;
        }

        protected void RadButtonSave_Click(object sender, EventArgs e)
        {
            BSCEntities context = new BSCEntities();
            //context.Attach(selectedUser);
            User user = (from u in context.Users
                         where u.ID == selectedUser.ID
                         select u).SingleOrDefault();

            //User u = selectedUser;
            //context.Attach(selectedUser);

            

            selectedUser.Phone = RadTextBoxCurrentPhone.Text;
            selectedUser.Email = RadTextBoxCurrentEmail.Text;
            selectedUser.Birthdate = RadDatePickerCurrentBirthday.SelectedDate;


            SaveImage();

            user.Phone = selectedUser.Phone;
            user.Email = selectedUser.Email;
            user.Birthdate = selectedUser.Birthdate;
            user.Photo = selectedUser.Photo;

            this.DataBind();

            try
            {
                context.SaveChanges();
            }
            catch
            {
                throw new InconsistentDatabaseException("Couldn't save image");
            }

            SwitchVisibility();
        }

        protected void RadButtonCancel_Click(object sender, EventArgs e)
        {
            SwitchVisibility();
        }

        
    }
}